package br.com.prontuario.comparator;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class MapComparatorValue {
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map sortCrescente(Map unsortMap) {
		List lista = new LinkedList(unsortMap.entrySet());
 
		// Ordenar a lista baseado em um comparator.
		Collections.sort(lista, new Comparator() {
			public int compare(Object o1, Object o2) {
				return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
			}
		});
 
		// Voltando a lista para o Map.
		Map sortedMap = new LinkedHashMap();
		for (Iterator it = lista.iterator(); it.hasNext();) {
			Map.Entry entry = (Map.Entry) it.next();
			sortedMap.put(entry.getKey(), entry.getValue());
		}
		return sortedMap;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map sortDecrescente(Map unsortMap) {
		List lista = new LinkedList(unsortMap.entrySet());
		
		// Ordenar a lista baseado em um comparator.
		Collections.sort(lista, new Comparator() {
			public int compare(Object o1, Object o2) {
				return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue());
			}
		});
		
		// Voltando a lista para o Map.
		Map sortedMap = new LinkedHashMap();
		for (Iterator it = lista.iterator(); it.hasNext();) {
			Map.Entry entry = (Map.Entry) it.next();
			sortedMap.put(entry.getKey(), entry.getValue());
		}
		return sortedMap;
	}
}